var url;
var xmlHttpRequest;
function getXMLHttpRequest(){
	var obj = null;
	if (window.XMLHttpRequest)
	  {// code for all new browsers
		obj=new XMLHttpRequest();
	  }
	else if (window.ActiveXObject)
	  {// code for IE5 and IE6
		obj = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	return obj;
}
window.onload=function(){
	try{
		document.charset="UTF-8";
		xmlHttpRequest = getXMLHttpRequest();
		url = "/RavioWeb/GetMessageServlet?userId="+document.getElementById("messageForm:userIdBox").value;
		xmlHttpRequest.onreadystatechange = function() { 
			if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status ==200){
				try{
					var str = xmlHttpRequest.responseText; 
					appendText(str);
				}catch(e){
					alert("2");
					alert(e);
				}
			}
		};
		setInterval("getMessage()",5000);
	}catch(e){
		alert("1");
		alert(e);
	}
};
function getMessage(){
	try{
		xmlHttpRequest.open("GET",url);
		xmlHttpRequest.send(null);
	}catch(e){
		alert(3);
		alert(e.message);
	}
}

var sendUrl = "/RavioWeb/SendMessageServlet";
function sendMessage(){
	try{
		var req = getXMLHttpRequest();
		var message = document.getElementById("messageForm:sendBox").value;
		var friendId = document.getElementById("friendList");
		friendId = friendId.options[friendId.selectedIndex].value;
		req.open("POST",sendUrl,true);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send("to="+friendId+"&content="+message);
	}catch(e){
		alert(e.message);
	}	
}

function appendText(text){
	var msg=document.getElementById("messageBox");
	msg.innerHTML = msg.innerHTML+text;
}